package cn.yuanqiao.cqbc.inspection.taskClass;

import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import cn.yuanqiao.cqbc.inspection.domain.RegularInspections;

import cn.yuanqiao.cqbc.inspectionLog.domain.InspectionLog;
import cn.yuanqiao.cqbc.inspectionLog.domain.LogDetails;
import cn.yuanqiao.cqbc.inspectionLog.mapper.InspectionLogMapper;
import cn.yuanqiao.cqbc.inspectionLog.mapper.LogDetailsMapper;
import cn.yuanqiao.cqbc.timedTasks.taskClass.taskListService.TaskInspectionContext;
import cn.yuanqiao.cqbc.timedTasks.taskClass.taskListService.TaskListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: TaskInspection
 * @Description: 类的描述
 * @author: lihuahua
 * @date: 2022/11/11 19:43
 * @Blog:
 */

@Service
@Slf4j
@Scope("prototype")
public class TaskInspection implements Runnable{

    private RegularInspections param;

    @Autowired
    private TaskInspectionContext taskInspectionContext;

    @Resource
    private InspectionLogMapper inspectionLogMapper;


    @Resource
    private LogDetailsMapper logDetailsMapper;


    public void taskInspection(RegularInspections param) {
        this.param = param;
    }

    @Override
    public void run() {
        try {
            if(null!=param){
                log.info("==============param：：：{}",param.toString());
            }

            Long id= SnowflakeIdGenerator.generateId();
            InspectionLog inspectionLog = new InspectionLog();
            inspectionLog.setId(id);
            inspectionLog.setTaskId(param.getId());
            inspectionLog.setTaskName(param.getTaskName());
            inspectionLog.setStatus("1");//任务状态 1 执行中 2 已完成 3 执行失败
            inspectionLog.setStartTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            inspectionLog.setTableName(param.getTenantCode().toUpperCase()+"_YQ_INSPECTION_LOG");
            int insert = inspectionLogMapper.insertInspectionLog(inspectionLog);
            if (insert == 1) {
                TaskListService taskList1 = taskInspectionContext.getTaskListService("inspection");
                if(null!=param){
                }
                log.info("==============开始执行任务==============");
                Map<String, Object> task = taskList1.task(inspectionLog,param);
                inspectionLog.setStatus("2");
                inspectionLog.setEndTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
                inspectionLog.setMessage(task.get("msg").toString());
                int i = inspectionLogMapper.updateInspectionLog(inspectionLog);
                if (task.get("detail") != null) {
//                    StringBuffer stringBuffer = new StringBuffer();
//                    List<YqTaskRunningDetail> detailList = (List<YqTaskRunningDetail>) task.get("detail");
//                    for (YqTaskRunningDetail item : detailList) {
//                        yqTaskRunningDetailMapper.insert(item);
//                        stringBuffer.append("巡检了：").append(item.getDataPath()).append("下的<")
//                                .append(item.getDataName()).append(">数据,检测完成;");
//                    }
//                    yqNoticeManage.setDetail(stringBuffer.toString());
                    List<LogDetails> detailList = (List<LogDetails>) task.get("detail");
                    if(detailList!=null&&detailList.size()>0) {
                        logDetailsMapper.batchInsertLogDetails(detailList);
                    }

                }
//                yqNoticeManage.setState("2");
//                yqNoticeManageMapper.updateById(yqNoticeManage);
//                log.info(task.get("msg").toString());

            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        //timingLogService.export(param.getInputUrl(),param.getDctype(),param.getOutputUrl(),param.getTaskName(),param.getTaskNumber().toString(),response,param.getCreateUser());
    }
}
